const path = require('path')
const multer = require('multer')
const getTime = require('./get_time')

const randomString = (len) => {
  len = len || 32
  const $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'    /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
  const maxPos = $chars.length
  let pwd = ''
  for (i = 0; i < len; i++) {
    pwd += $chars.charAt(Math.floor(Math.random() * maxPos))
  }

  return pwd
}

const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, path.join(__dirname, '../../', 'images'));
  },
  filename: (req, file, cb) => {
    cb(null, `${randomString(6)}@${getTime()}.jpg`);
  }
});

const upload = multer({ storage }).array('file', 4);

module.exports = upload